#=============================================================================#
#
# PROJECT:        Who Pays for Peace?
# AUTHORS:        ** anonymized for review **
# CONTACT:        ** anonymized for review **
# LAST MODIFIED:  July 5, 2022
# 
#=============================================================================#
#
# This R file contains the code used to replicate the conditional treatment effects 
# (at Wave 1)
# 
#=============================================================================#


# Initial settings ------------------------------------------------------------

#rm(list=ls())
#getwd()

## Install and load all necessary packages ------------------------------------
# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.

ipak <- function(pkg){  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("ggplot2", "ggthemes", "interplot", "sjPlot", "ggpubr") 
ipak(packages)


## Load the data --------------------------------------------------------------
mydata <- readRDS("data/T1-clean-data.rds")


# Conditional Average Treatment Effects ---------------------------------------

## Mechanism 1: Ingroup love --------------------------------------------------
summary(mydata$national_pride)
mydata$national_pride_2 <- ifelse(mydata$national_pride == 11, 1, 0)
mydata$national_pride_2 <- as.factor(mydata$national_pride_2)
summary(mydata$national_pride_2)

### Compensations
CATE_c_ingroup <- lm(compensation ~ comp_condition * national_pride_2, mydata)
summary(CATE_c_ingroup)

CATE_c_1 <- 
  interplot(CATE_c_ingroup, "comp_conditionAzerbaijan", "national_pride_2",
            facet_labs = "") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('National pride\n ("ingroup love")\n') +
  ylab('Marginal effect\n of ingroup treatment') + 
  theme_classic() + 
  theme(strip.background = element_blank())
CATE_c_1

CATE_c_2 <- 
  interplot(CATE_c_ingroup, "comp_conditionArmenia", "national_pride_2",
            facet_labs = "") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('National pride\n ("ingroup love")\n') +
  ylab('Marginal effect of\n outgroup treatment') + 
  theme_classic() + 
  theme(strip.background = element_blank())
CATE_c_2

CATE_c_ingroup <- ggarrange(CATE_c_1, CATE_c_2) 
CATE_c_ingroup <- annotate_figure(CATE_c_ingroup,
                                  top = text_grob("Monetary Compensation", 
                                                  size = 12))
CATE_c_ingroup

### Punishments
CATE_p_ingroup <- lm(punishment ~ punish_condition * national_pride_2, mydata)
summary(CATE_p_ingroup)

CATE_p_1 <- 
  interplot(CATE_p_ingroup, "punish_conditionAzerbaijan", "national_pride_2",
            facet_labs = "") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('National pride\n ("ingroup love")\n') +
  ylab('Marginal effect\n of ingroup treatment') + 
  theme_classic() + 
  theme(strip.background = element_blank())
CATE_p_1

CATE_p_2 <- 
  interplot(CATE_p_ingroup, "punish_conditionArmenia", "national_pride_2",
            facet_labs = "") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('National pride\n ("ingroup love")\n') +
  ylab('Marginal effect of\n outgroup treatment')  + 
  theme_classic() + 
  theme(strip.background = element_blank())
CATE_p_2

CATE_p_ingroup <- ggarrange(CATE_p_1, CATE_p_2) 
CATE_p_ingroup <- annotate_figure(CATE_p_ingroup,
                                  top = text_grob("War Crime Punishments", 
                                                  size = 12))
CATE_p_ingroup

### Combined plot for ingroup love
CATE_ingroup <- ggarrange(CATE_c_ingroup, CATE_p_ingroup)
CATE_ingroup <- annotate_figure(CATE_ingroup, 
                                top = text_grob("", size = 14),
                                fig.lab = "PANEL A: Moderating Impact of Ingroup Love", fig.lab.face = "bold", fig.lab.size = 14)
CATE_ingroup


## Mechanism 2: Outgroup hate -------------------------------------------------

### Compensations
CATE_c_outgroup <- lm(compensation ~ comp_condition * prejudice, mydata)
summary(CATE_c_outgroup)

CATE_c_1 <- 
  interplot(CATE_c_outgroup, "comp_conditionAzerbaijan", "prejudice") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('Prejudice\n ("outgroup hate")\n') +
  ylab('Marginal effect\n of ingroup treatment') + theme_classic() 
CATE_c_1

CATE_c_2 <- 
  interplot(CATE_c_outgroup, "comp_conditionArmenia", "prejudice") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('Prejudice\n ("outgroup hate")\n') +
  ylab('Marginal effect of\n outgroup treatment') + theme_classic()
CATE_c_2

CATE_c_outgroup <- ggarrange(CATE_c_1, CATE_c_2) 
CATE_c_outgroup <- annotate_figure(CATE_c_outgroup,
                                  top = text_grob("Monetary Compensation\n", 
                                                  size = 12))
CATE_c_outgroup

### Punishments
CATE_p_outgroup <- lm(punishment ~ punish_condition * prejudice, mydata)
summary(CATE_p_outgroup)

CATE_p_1 <- 
  interplot(CATE_p_outgroup, "punish_conditionAzerbaijan", "prejudice") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('Prejudice\n ("outgroup hate")\n') +
  ylab('Marginal effect\n of ingroup treatment') + theme_classic() 
CATE_p_1

CATE_p_2 <- 
  interplot(CATE_p_outgroup, "punish_conditionArmenia", "prejudice") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab('Prejudice\n ("outgroup hate")\n') +
  ylab('Marginal effect of\n outgroup treatment') + theme_classic()
CATE_p_2

CATE_p_outgroup <- ggarrange(CATE_p_1, CATE_p_2) 
CATE_p_outgroup <- annotate_figure(CATE_p_outgroup,
                                  top = text_grob("War Crime Punishments\n", 
                                                  size = 12))
CATE_p_outgroup

### Combined plot for outgroup hate
CATE_outgroup <- ggarrange(CATE_c_outgroup, CATE_p_outgroup)
CATE_outgroup <- annotate_figure(CATE_outgroup, 
                                top = text_grob("", size = 14),
                                fig.lab = "PANEL B: Moderating Impact of Outgroup Hate", fig.lab.face = "bold", fig.lab.size = 14)
CATE_outgroup


## Alternative Explanation: Threat perceptions -------------------------------- 

### Compensations
CATE_c_threat <- lm(compensation ~ comp_condition * threat, mydata)
summary(CATE_c_threat)

CATE_c_1 <- 
  interplot(CATE_c_threat, "comp_conditionAzerbaijan", "threat") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab("Threat perceptions\n") +
  ylab('Marginal effect\n of ingroup treatment') + theme_classic() 
CATE_c_1

CATE_c_2 <- 
  interplot(CATE_c_threat, "comp_conditionArmenia", "threat") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab("Threat perceptions\n") +
  ylab('Marginal effect of\n outgroup treatment') + theme_classic()
CATE_c_2

CATE_c_threat <- ggarrange(CATE_c_1, CATE_c_2) 
CATE_c_threat <- annotate_figure(CATE_c_threat,
                                   top = text_grob("Monetary Compensation\n", 
                                                   size = 12))
CATE_c_threat

### Punishments
CATE_p_threat <- lm(punishment ~ punish_condition * threat, mydata)
summary(CATE_p_threat)

CATE_p_1 <- 
  interplot(CATE_p_threat, "punish_conditionAzerbaijan", "threat") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab("Threat perceptions\n") +
  ylab('Marginal effect\n of ingroup treatment') + theme_classic() 
CATE_p_1

CATE_p_2 <- 
  interplot(CATE_p_threat, "punish_conditionArmenia", "threat") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab("Threat perceptions\n") +
  ylab('Marginal effect of\n outgroup treatment') + theme_classic()
CATE_p_2

CATE_p_threat <- ggarrange(CATE_p_1, CATE_p_2) 
CATE_p_threat <- annotate_figure(CATE_p_threat,
                                   top = text_grob("War Crime Punishments\n", 
                                                   size = 12))
CATE_p_threat

### Combined plot for threat perceptions
CATE_threat <- ggarrange(CATE_c_threat, CATE_p_threat)
CATE_threat <- annotate_figure(CATE_threat, 
                                 top = text_grob("", size = 14),
                                 fig.lab = "PANEL C: Moderating Impact of Threat Perceptions", fig.lab.face = "bold", fig.lab.size = 14)
CATE_threat


#### > Combined CATE plot -----------------------------------------------------
jpeg("figures/fig3.jpg", width = 9.5, height = 11, units='in', res = 300)
ggarrange(CATE_ingroup, CATE_outgroup, CATE_threat,
          nrow = 3, ncol = 1)
dev.off()

